package com.zdp.filesync.configuration;

import com.zdp.filesync.commonline.service.CommonLine;
import com.zdp.filesync.commonline.service.impl.CommonLineUnix;
import com.zdp.filesync.commonline.service.impl.CommonLineWin;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.*;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

import java.util.function.Consumer;

@Configuration
public class FileSyncConfiguration {

    @Bean
    @Conditional(WindowsCondition.class)
    public CommonLine commonLineWin(){
        return new CommonLineWin();
    }

    @Bean
    @Conditional(LinuxCondition.class)
    public CommonLine commonLineUnix(){
        return new CommonLineUnix();
    }

}

class WindowsCondition  implements Condition{

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata annotatedTypeMetadata) {
        //获取环境信息  封装了当前运行时的一些信息，包括环境变量，虚拟机的一些变量等
        Environment environment = context.getEnvironment();

        String property = environment.getProperty("os.name");
        if(property.toLowerCase().startsWith("windows")){
            return true;
        }
        return false;
    }
}

class LinuxCondition implements Condition {

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata annotatedTypeMetadata) {
        Environment environment = context.getEnvironment();
        String property = environment.getProperty("os.name");
        if(property.toLowerCase().startsWith("linux")){
            return true;
        }
        return false;
    }
}
